package cn.tedu.csmall.passport.service.impl;

import cn.tedu.csmall.passport.ex.ServiceException;
import cn.tedu.csmall.passport.mapper.AdminMapper;
import cn.tedu.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.csmall.passport.pojo.entity.Admin;
import cn.tedu.csmall.passport.pojo.vo.AdminListVO;
import cn.tedu.csmall.passport.service.IAdminService;
import cn.tedu.csmall.passport.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理管理员数据的业务实现类
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AuthenticationManager authenticationManager;


    @Override
    public void login(AdminLoginDTO adminLoginDTO) {
        log.debug("开始处理【登录管理员】的业务，参数：{}",adminLoginDTO);
        //执行认证
        Authentication authentication = new UsernamePasswordAuthenticationToken(
                adminLoginDTO.getUsername(),adminLoginDTO.getPassword()
        );
        Authentication authenticateResult = authenticationManager.authenticate(authentication);
        log.debug("认证通过，结果：{}",authenticateResult);
        //将认证信息存入到SecurityContext
        SecurityContext securityContext = SecurityContextHolder.getContext();
        securityContext.setAuthentication(authenticateResult);
    }

    @Override
    public void addNew(AdminAddNewDTO adminAddNewDTO) {
        log.debug("开始处理【添加管理员】的业务，参数：{}",adminAddNewDTO);
        //检查管理员用户名，手机号，电子邮箱是否被占用
        String username = adminAddNewDTO.getUsername();
        String phone = adminAddNewDTO.getPhone();
        String email = adminAddNewDTO.getEmail();

        int countByUsername = adminMapper.countByUsername(username);
        if (countByUsername > 0){
            //如果已经占用则抛出异常
            String message = "添加管理员失败，管理员用户名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int countByPhone = adminMapper.countByPhone(phone);
        if (countByPhone > 0){
            //如果已经占用则抛出异常
            String message = "添加管理员失败，管理员手机号已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int countByEmail = adminMapper.countByEmail(email);
        if (countByEmail > 0){
            //如果已经占用则抛出异常
            String message = "添加管理员失败，管理员电子邮箱已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //创建Admin对象
        Admin admin = new Admin();
        //补全Admin对象中的属性值：来自参数adminAddNewDTO
        BeanUtils.copyProperties(adminAddNewDTO,admin);
        log.debug("即将插入数据，参数：{}",admin);
        int rows = adminMapper.insert(admin);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后在尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public List<AdminListVO> list() {
        log.debug("开始处理【查询管理员列表】的业务，参数：无");
        List<AdminListVO> list = adminMapper.list();
        return list;
    }
}
